//
//  SensorTagApplicationViewController.m
//
//  Created by Ole Andreas Torvmark on 9/25/12.
//  Copyright (c) 2012 Texas Instruments. All rights reserved.
//

#import "SensorTagApplicationViewController.h"
#import "CmdData.h"
#import "JianBianViewController.h"
#import "TiaoBianViewController.h"
#import "KZDefaultColorViewController.h"
#import "AppDelegate.h"
#import "ColorArrayViewController.h"
@interface SensorTagApplicationViewController ()

@end

@implementation SensorTagApplicationViewController

-(id) initWithStyle:(UITableViewStyle)style{
    self = [super initWithStyle:style];
    if (self) {
        self.functionName=[NSArray arrayWithObjects:
                           @"选择颜色", @"设置颜色变化组", @"灯泡名字", @"设置密码", @"渐变效果", @"跳变效果",@"删除所有配对信息",@"一键设置白光",nil];
    }
    [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(alphaFader:) userInfo:nil repeats:YES];
    return self;
}

- (void)viewDidAppear:(BOOL)animated {
}


-(void)viewWillDisappear:(BOOL)animated {
}

-(void)viewDidDisappear:(BOOL)animated {
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //  NSString *cellType = [self.sensorsEnabledArray objectAtIndex:indexPath.row];
    return 50;
}

-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"此软件仅仅是用来演示使用, 没有增加各种兼容测试.";
    }
    return @"";
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.functionName.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    NSString *str=self.functionName[indexPath.row];
    cell.textLabel.text = str;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //// 产品图片
    return cell;
}


- (IBAction) handleCalibrateMag {
    NSLog(@"Calibrate magnetometer pressed !");
}
- (IBAction) handleCalibrateGyro {
    NSLog(@"Calibrate gyroscope pressed ! ");
    
}

-(void) alphaFader:(NSTimer *)timer {
}

#pragma mark -下一级页面push/show方法.
-(void)pushColorArray
{
    ColorArrayViewController *colorVC=[[ColorArrayViewController alloc] init];
    [self.navigationController pushViewController:colorVC animated:YES];
    colorVC.title=@"选择颜色组";
}
-(void) pushColorPad
{
    KZDefaultColorViewController *colorVC=[[KZDefaultColorViewController alloc] init];
    [self.navigationController pushViewController:colorVC animated:YES];
    colorVC.title=@"选择颜色";
}
-(void) showInputPassWd
{
    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"输入设备密码" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
    
    alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
    [alertView textFieldAtIndex:0].placeholder=@"密码最大6个数字";
    [alertView textFieldAtIndex:0].keyboardType=UIKeyboardTypeNumberPad;
    [alertView show];
    alertView.tag=1;
}
-(void) showInputName
{
    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"输入设备名字" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
    
    alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
    [alertView textFieldAtIndex:0].placeholder=@"设备名字最大12个字符或6个汉字";
    [alertView show];
    alertView.tag=2;
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==1)  //密码
    {
        if (buttonIndex==1){  //确定按钮
            NSString *str=[alertView textFieldAtIndex:0].text;
            NSLog(@"return  密码确定 %@",str);
            //发送出去.
            NSData *data=[CmdData getPassWdCmdArray:str];
            [BLEUtility writeData:data];
        }
    }else if(alertView.tag==2){
        if (buttonIndex==1) {
            NSString *str=[alertView textFieldAtIndex:0].text;
            NSLog(@"return  名字确定 %@",str);
            //发送出去.
            NSData *data=[CmdData getNameCmdArray:str];
            [BLEUtility writeData:data];
        }
    }
}
-(void)pushJianBian
{
    JianBianViewController *jianBianVC=[[JianBianViewController alloc] initWithNibName:@"JianBianView" bundle:nil];
    [self.navigationController pushViewController:jianBianVC animated:YES];
}
-(void)pushTiaoBian
{
    TiaoBianViewController *tiaobianVC=[[TiaoBianViewController alloc] initWithNibName:@"TiaoBianView" bundle:nil];
    [self.navigationController pushViewController:tiaobianVC animated:YES];
    tiaobianVC.title=@"演示跳变效果";
}
-(void)deleteBondPasswd
{
    [BLEUtility deleteBondPasswd];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"已连接设备的绑定信息已删除,请重新连接." message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}
-(void)oneKeySetWhite
{
    NSData *data=[CmdData getOneKeyWhiteArray];
    [BLEUtility writeData:data];
    [self.view makeToast2:@"已发送一键白光"];
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0: //选择颜色.
              [self pushColorPad];
            break;
        case 1:  //设置颜色变化组
            [self pushColorArray];
            break;
        case 2:  //灯泡名字
            [self showInputName];
            break;
        case 3: //设置密码
            [self showInputPassWd];
            break;
        case 4: //渐变效果
            [self pushJianBian];
            break;
        case 5:  //跳变效果
            [self pushTiaoBian];
            break;
        case 6:
            [self deleteBondPasswd];
        case 7:
            [self oneKeySetWhite];
            break;
        default:
            break;
    }
}


@end
